/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import com.cleangwt.bootstrap.client.ui.Alert;
import com.cleangwt.bootstrap.client.ui.Alert.AlertType;
import com.cleangwt.bootstrap.client.ui.Modal;
import com.cleangwt.bootstrap.client.ui.Popover;
import com.cleangwt.bootstrap.client.ui.Popover.PopoverType;
import com.cleangwt.bootstrap.client.ui.Tooltip;
import com.cleangwt.bootstrap.client.ui.Tooltip.TooltipType;
import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.showcase.client.resource.i18n.BaseWidgetMessages;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.TextBox;
import com.cleangwt.user.client.ui.ValueCallback;
import com.cleangwt.user.shared.Option;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.BASE_WIDGET)
@CodeSplit
@Page
public class PopupDemo extends ShowcasePage {

  private BaseWidgetMessages c = GWT.create(BaseWidgetMessages.class);

  public PopupDemo() {
    super(
        "The Popover, Alert & Modal",
        "Integrate bootstrap's popver, alert and modal styles. Refer to <a href='http://getbootstrap.com/javascript/#modals'>Bootstrap</a>",
        "PopupDemo.java");

    FlowPanel layout = new FlowPanel();
    Popover popover = new Popover("The Custom Popover", PopoverType.RIGHT);
    Button btnPopover1 = new Button("Toggle Left");
    Button btnPopover2 = new Button("Toggle Top");
    Button btnPopover3 = new Button("Toggle Bottom");
    Button btnPopover4 = new Button("Toggle Right");
    Alert alert = new Alert(AlertType.WARNING, true);
    Button btnAlert = new Button("Show a Default Alert");
    Button btnConfirm = new Button(c.confirm());
    Button btnInput = new Button(c.input());
    Button btnOption = new Button(c.options());
    final TextBox txtfInput = new TextBox();
    Tooltip tooltip = new Tooltip("The Custom tooltip", TooltipType.RIGHT);

    this.setContentWidget(layout);
    layout.add(Lay.heading("Popovers"));
    layout.add(Lay.p(popover));
    layout.add(Lay.p(btnPopover1, btnPopover2, btnPopover3, btnPopover4));
    // alerts
    layout.add(Lay.heading("Alerts"));
    layout.add(alert);
    layout.add(btnAlert);
    // modals
    layout.add(Lay.heading("Modals"));
    layout.add(Lay.p(btnConfirm, btnOption, btnInput));
    // tooltips
    layout.add(Lay.heading("Tooltips"));
    layout.add(tooltip);

    alert.setWidget(new HTML("The custom alert!"));
    // activate
    Popover.activate(btnPopover1.getElement(), "Title", "Popover on left", PopoverType.LEFT, false);
    Popover.activate(btnPopover2.getElement(), "Title", "Popover on top", PopoverType.TOP, false);
    Popover.activate(btnPopover3.getElement(), "Title", "Popover on bottom", PopoverType.BOTTOM,
        false);
    Popover.activate(btnPopover4.getElement(), "Title", "Popover on right", PopoverType.RIGHT,
        false);

    Tooltip.activate(tooltip.getElement(), "The custom tooltip", TooltipType.RIGHT);

    btnAlert.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        Alert.alert("The default alert " + Document.get().createUniqueId());
      }
    });

    btnConfirm.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        Modal.confirm(c.confirm(), new ValueCallback<String>() {

          @Override
          public void onValueCallback(String option) {
            Modal.message(c.certainly());
          }
        });
      }
    });

    btnInput.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        Modal.option(c.ask(), txtfInput, new ValueCallback<String>() {

          @Override
          public void onValueCallback(String option) {
            Modal.message(c.hello(txtfInput.getText()));
            txtfInput.setValue("");
          }
        });
      }
    });

    btnOption.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {

        Modal.option(new HTML("Choose an option!"), new Option[] {
            new Option("Y"), new Option("N"), new Option("Cancel")}, new ValueCallback<String>() {

          @Override
          public void onValueCallback(String option) {
            Modal.message("Your option is " + option);
          }
        });
      }
    });

  }

}